package com.xhwl.centiotdata.suppliersystem.leopen;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.LeopenConstant;
import com.xhwl.centiotdata.pojo.dto.dahua.UserLeopenDTO;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.common.dto.cent.sdata.AccountDTO;
import com.xhwl.common.pojo.cent.sdata.personnel.StaffDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户
 *
 * @author pengjunming
 * @description
 * @date 2023/2/11 14:42
 */
@Service
@Slf4j
public class UserLeopenHandler {

    @Resource
    private GeneralLeopenHandler generalLeopenHandler;

    public String addUser(PedestrianIntegrationConfigDTO config, StaffDTO dto) {
        try {
            UserLeopenDTO userLeopenDTO = getUserLeopenDTO(dto);
            JSONObject jsonObject = generalLeopenHandler.onPosting(config.getAk(), config.getSk(),
                    config.getPassword(), config.getIpAndPort(), LeopenConstant.LENIU_ACCOUNT_ADD, Collections.singletonList(userLeopenDTO));
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("successList");
            if (jsonArray == null || jsonArray.isEmpty()) {
                log.error("新增乐牛账户异常：{}", JSON.toJSONString(jsonObject));
                return null;
            }
            upload_img(config, dto);
            return jsonArray.getJSONObject(0).getString("custThirdId");
        } catch (Exception e) {
            log.error("新增账户异常", e);
            return null;
        }
    }

    public void updateUser(PedestrianIntegrationConfigDTO config, StaffDTO dto) {
        try {
            UserLeopenDTO userLeopenDTO = getUserLeopenDTO(dto);
            generalLeopenHandler.onPosting(config.getAk(), config.getSk(),
                    config.getPassword(), config.getIpAndPort(), LeopenConstant.LENIU_ACCOUNT_UPDATE, userLeopenDTO);
            upload_img(config, dto);
        } catch (Exception e) {
            log.error("新增账户异常", e);
        }
    }

    public void deleteUser(PedestrianIntegrationConfigDTO config, AccountDTO accountDTO) {
        try {
            Map<String, Object> content = new HashMap<>();
            content.put("custThirdId", accountDTO.getId());
            content.put("custNum", accountDTO.getId());
            content.put("serialNum", "");
            content.put("ifRefundDeposit", 2);
            content.put("ifRefundCost", 2);
            generalLeopenHandler.onPosting(config.getAk(), config.getSk(),
                    config.getPassword(), config.getIpAndPort(), LeopenConstant.LENIU_ACCOUNT_DELETE, content);
        } catch (Exception e) {
            log.error("新增账户异常", e);
        }
    }

    public void upload_img(PedestrianIntegrationConfigDTO config, StaffDTO dto) {
        try {
            Map<String, Object> content = new HashMap<>();
            content.put("custThirdId", dto.getAccountId());
            content.put("custNum", dto.getAccountId());
            content.put("url", dto.getFaceUrl());
            generalLeopenHandler.onPosting(config.getAk(), config.getSk(),
                    config.getPassword(), config.getIpAndPort(), LeopenConstant.LENIU_ACCOUNT_IMAGE, content);
        } catch (Exception e) {
            log.error("上传人脸照片异常", e);
        }

    }

    private UserLeopenDTO getUserLeopenDTO(StaffDTO dto) {
        String thirdId = dto.getAccountId().toString();
        return UserLeopenDTO.builder()
                .custThirdId(thirdId).custNum(thirdId).custName(dto.getName())
                .orgNum(CollectionUtils.isEmpty(dto.getOrgIds()) ? "" : dto.getOrgIds().get(0).toString())
                .age(dto.getAge() == null ? null : dto.getAge().toString()).mobile(dto.getPhone())
                .sex(dto.getSex() == 0 ? "2" : "1")//星启性别：0女；1男 乐牛性别 1-男 2-女
                .idCard(dto.getIdCard()).email(dto.getEmail()).build();
    }
}
